module.exports = function () {
    var sequelize = require("../libs/db").sequelize;
    var Access = sequelize.import("../models/iframe_tabelid");
    /**
     * 这是个实例
     */
    var GetID = async function (TableName, cb) {
        var Do = async function (cb) {
            try {
                var newId = 0;
                var data = await Access.findById(TableName);
                if (data) {
                    newId = data.IDValue + 1;
                    await Access.update({
                        IDValue: newId
                    }, {
                        where: {
                            TabelName: TableName
                        }
                    });
                } else {
                    var data = {};
                    newId = 1;
                    data.TabelName = TableName;
                    data.IDValue = newId;
                    await Access.create(data);
                }
                if (cb) cb(null, newId);
            } catch (err) {
                if (cb) cb(err);
            }

        };

        if (cb) {
            Do(cb);
        } else {
            return new Promise(function (resolve, reject) {
                Do(function (err, ret) {
                    if (err) {
                        console.log(err);
                        reject(err);
                    } else {
                        resolve(ret);
                    }
                })
            })
        }
    }

    return {
        Access: Access,
        GetID: GetID
    }
}();